package com.hbyj.struts.action;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.Jbqk01;
import com.hbyj.hbm.vo.Jbqk01Id;
import com.hbyj.hbm.vo.YjdcYjdc01;
import com.hbyj.hbm.vo.YjdcYjdc06;
import com.opensymphony.xwork2.ActionSupport;

public class YjdclrAction  extends ActionSupport{
	
	//对象
	private YjdcYjdc01 yjdc01;
	
	private File qmlj;
	private File jubu1;
	private File jubu2;
	

	public YjdcYjdc01 getYjdc01() {
		return yjdc01;
	}

	public void setYjdc01(YjdcYjdc01 yjdc01) {
		this.yjdc01 = yjdc01;
	}

	public File getQmlj() {
		return qmlj;
	}

	public void setQmlj(File qmlj) {
		this.qmlj = qmlj;
	}

	public File getJubu1() {
		return jubu1;
	}

	public void setJubu1(File jubu1) {
		this.jubu1 = jubu1;
	}

	public File getJubu2() {
		return jubu2;
	}

	public void setJubu2(File jubu2) {
		this.jubu2 = jubu2;
	}
	
	public void queryYJDCDByKeyWord() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String lyCode = request.getParameter("lyCode");
		String keyword = request.getParameter("keyword");
		String hql = "select JBQK0100,JBQK0110,JBQK0102 from JBQK01 where JBQK0100 like '"+lyCode+"' And JBQK0110 like '%"+keyword+"%'";
		List list = DbTools.findBySql(hql);
		if(list.size()>0){
			JSONArray jsonArray = new JSONArray();
			for(int i=0;i<list.size();i++){
				JSONObject obj = new JSONObject();
				Object[] object = (Object[]) list.get(i);
				obj.accumulate("id",object[0]);
				obj.accumulate("name",object[1]);
				obj.accumulate("biaoshi", object[2]);
				jsonArray.add(obj);
			}
			ServletActionContext.getResponse().getOutputStream().write(jsonArray.toString().getBytes("utf-8"));
		}
	}
	
	public void insertYjdcdForFWQ() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String jbqk0110 = request.getParameter("jbqk0110");
		String jbqk0102 = request.getParameter("jbqk0102");
		String yjdc01000 = request.getParameter("yjdc01000");
		String yjdc01140 = request.getParameter("yjdc01140");
		String yjdc01330 = request.getParameter("yjdc01330");
		yjdc01.setYjdc01000(yjdc01000);
		yjdc01.setYjdc01140(yjdc01140);
		yjdc01.setYjdc01330(yjdc01330);
		yjdc01.setJbqk0102(jbqk0102);
		yjdc01.setYjdc01380("false");
		//对图片的处理
		if(qmlj!=null){
			String newFileName = changeFileName();
			if(uploadFile(newFileName,qmlj)){//上传成功之后
				yjdc01.setYjdc01300(newFileName);
			}
		}
		if(jubu1!=null){
			String newFileName = changeFileName();
			if(uploadFile(newFileName,jubu1)){//上传成功之后
				yjdc01.setYjdc01310(newFileName);
			}
		}
		if(jubu2!=null){
			String newFileName = changeFileName();
			if(uploadFile(newFileName,jubu2)){//上传成功之后
				yjdc01.setYjdc01320(newFileName);
			}
		}
		boolean flag = checkJbqk01CaseYjdclr(yjdc01000);
		if(!flag){//表示不存在，插入一条应急调查点到jbqk01
			Jbqk01Id id = new Jbqk01Id(yjdc01.getYjdc01000(), "3");
			Jbqk01 jbqk = new Jbqk01();
			jbqk.setId(id);
			jbqk.setJbqk0110(jbqk0110);
			jbqk.setJbqk0120(Double.valueOf(yjdc01.getYjdc01350()));
			jbqk.setJbqk0130(Double.valueOf(yjdc01.getYjdc01360()));
			jbqk.setJbqk0160(yjdc01.getYjdc01030());
			jbqk.setJbqk0170(yjdc01.getYjdc01300());
			jbqk.setJbqk0180(yjdc01.getYjdc01310());
			jbqk.setJbqk0190(yjdc01.getYjdc01320());
			if(DbTools.saveObj(jbqk)){
				
			}else{
				ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
				return;
			}
		}
		if(DbTools.saveObj(yjdc01)){
			ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		}else{
			ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
		}
	}

	public boolean checkJbqk01CaseYjdclr(String tybh){
		boolean flag = false;
		String hql = "from Jbqk01 where id.jbqk0100='"+tybh+"' and id.jbqk0102='3'";
		List list = DbTools.findByHql(hql);
		if(list.size()>0){
			//表示已经存在了
			flag = true;
		}
		return flag;
	}
	
	//更改文件的名称
	public String changeFileName(){
		String newFileName = "";
		DateFormat format = new SimpleDateFormat("yyMMddHHmmssSSS");
        String formatDate = format.format(new java.util.Date());
        newFileName  = formatDate +".jpg";
		return newFileName;
	}
	
	
	//将上传的文件放到指定的目录下面
	public boolean uploadFile(String newFileName,File f){
		String serverPath="uploadfolder/simple/yjdc/image";
		String getFilePath = ServletActionContext.getServletContext().getRealPath("/"+serverPath);
		if(copyFile(getFilePath, newFileName ,f)){
			return true;
		}else{
			return false;
		}
	}
	private boolean copyFile(String uploadpath, String uploadFileFileName, File f) {
			String getfilePath = uploadpath;
	        String newfilename = uploadFileFileName;
	        //要存放的文件对象
	        File uploadFile  = new File(getfilePath+"/"+newfilename);
	        // 复制file对象，实现上传 
	        try {
				FileUtils.copyFile(f,uploadFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return true;
	}
}
